package com.easpeed.comon.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.jkt.tcompress.Compress
import java.io.File
import java.lang.RuntimeException


class ImageUtils {

    companion object {


        private var applicationContext: Context? = null


        public fun init(appContext: Context) {
            this.applicationContext = appContext
        }

        /**
         * 加载一张图片
         */
        @SuppressLint("CheckResult")
        fun loadImage(
            url: String?,
            holder: Int?,
            error: Int?,
            view: ImageView?,
        ) {
            checkInit()
            val newUrl = checkUrl(url)
            val builder = Glide.with(applicationContext!!)
                .asBitmap()
            if (holder != null) {
                builder.placeholder(holder)
            }
            if (error != null) {
                builder.error(error)
            }
            builder.load(newUrl)
            builder.into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    resource: Bitmap,
                    transition: Transition<in Bitmap>?,
                ) {
                    view?.setImageBitmap(resource)
                }

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                    error?.let {
                        view?.setImageResource(error)
                    }
                }

                override fun onLoadStarted(placeholder: Drawable?) {
                    super.onLoadStarted(placeholder)
                    holder?.let {
                        view?.setImageResource(holder)
                    }
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    // this is called when imageView is cleared on lifecycle call or for
                    // some other reason.
                    // if you are referencing the bitmap somewhere else too other than this imageView
                    // clear it here as you can no longer have the bitmap
                }
            })
        }

        private fun checkInit() {
            if (applicationContext == null) {
                throw RuntimeException("请在application中初始化init()")
            }
        }

        /**
         * 圆形图片
         *
         * @param imagePath 图片路径
         * @param imageView 图片控价
         */
        @SuppressLint("CheckResult")
        fun loadImageRound(
            imageView: ImageView, imagePath: String?, placeholder: Int, error: Int,
            transform: BitmapTransformation,
        ) {
            checkInit()
            val imagePath = checkUrl(imagePath)
            val glide = Glide.with(applicationContext!!).load(imagePath).transform(transform)
            if (placeholder == -1) {
                glide.placeholder(imageView.drawable).dontAnimate()
            } else {
                glide.placeholder(placeholder)
            }

            glide.error(error).into(imageView)
        }

        private fun checkUrl(url: String?): String {
            if (TextUtils.isEmpty(url)) {
                return ""
            }
            return url!!
        }

        fun compressImage(bitmap: Bitmap): File? {
            return try {
                // 压缩图片到指定输出文件
                val compress: Compress = Compress.Builder()
                    .setQuality(50)
                    .setFormat(Bitmap.CompressFormat.JPEG)
                    .setConfig(Bitmap.Config.RGB_565)
                    .build()
                val file = compress.compressedToFile(bitmap)
                return file
            } catch (e: Exception) {
                e.printStackTrace()
                return null
            }
        }

        fun compressImage(bitmap: File): File? {
            return try {
                // 压缩图片到指定输出文件
                val compress: Compress = Compress.Builder()
                    .setQuality(50)
                    .setFormat(Bitmap.CompressFormat.JPEG)
                    .setConfig(Bitmap.Config.RGB_565)
                    .build()
                val file = compress.compressedToFile(bitmap)
                return file
            } catch (e: Exception) {
                e.printStackTrace()
                return null
            }
        }
    }


}